/**
 * RunFunCursor.java V1.0 Oct 13, 2011 12:04:30 PM
 * 
 * Copyright 2011 AsiaInfo Linkage, All rights reserved.
 * 
 * Modification history(By Time Reason):
 * 
 * Description:
 */

package psn.ygn;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.TimeoutException;

/**
 * 功能描述：
 * 
 * @author 余根宁
 * 
 * <p>
 * 修改历史：(修改人，修改时间，修改原因/内容)
 * </p>
 */
public class RunFunCursor {
    
    private static int MAX_VALUE = 10000000;
    
    /**
     * 构造函数：
     * 
     */
    public RunFunCursor() {

    }
    
    /**
     * 
     * 功能描述：计算
     * 
     * @author 余根宁
     *         <p>
     *         创建日期 ：Oct 13, 2011 12:13:14 PM
     *         </p>
     * 
     * @param cnt
     * 
     * <p>
     * 修改历史 ：(修改人，修改时间，修改原因/内容)
     * </p>
     */
    public void run(int cnt) {

        ArrayList<Future<BigDecimal>> lst = new ArrayList<Future<BigDecimal>>();
//        /ArrayList<Callable<BigDecimal>> callLst = new ArrayList();
        ///先创建若干的线程；写入Future
        for (int i = 0; i < cnt; i++) {
            Callable<BigDecimal> call = new Callable<BigDecimal>() {
                public BigDecimal call() throws Exception {
                    long b1 = System.currentTimeMillis();
                    System.out.println(Thread.currentThread().getName() + ";begin;"+b1);
                    BigDecimal bg = BigDecimal.ZERO;
                    for (int j = 0; j < MAX_VALUE; j++) {
                        bg = bg.add(new BigDecimal(j));
                    }
                    long e1 = System.currentTimeMillis();
                    System.out.println(Thread.currentThread().getName() + ";end;"+e1+";cnt;"+(e1-b1));
                    return bg;
                }
            };
//            /callLst.add(call);
            lst.add(FunCursor.submit(call));
        }
        
        int j = 0;
        ///获取每个future的值；
        for (Future<BigDecimal> future : lst) {
            j++;
            try {
                BigDecimal bg = FunCursor.getFutureValue(future);
            } catch (TimeoutException err) {
                System.out.println(err.getMessage());
            } catch (Exception err) {
                System.out.println(err.getMessage());
            }
        }
    }
    
    public void runSy(int cnt) {

        for (int i = 0; i < cnt; i++) {
            long b1 = System.currentTimeMillis();
            System.out.println(i + ";begin;"+b1);
            BigDecimal bg = BigDecimal.ZERO;
            for (int j = 0; j < MAX_VALUE; j++) {
                bg = bg.add(new BigDecimal(j));
            }
            long e1 = System.currentTimeMillis();
            System.out.println(i + ";end;"+e1+";cnt;"+(e1-b1));
        }
    }
    
    public static void main(String[] args) {

        long begin = System.currentTimeMillis();
        System.out.println("Begin:" + begin);
        RunFunCursor cursor = new RunFunCursor();
        cursor.run(3);
        long end = System.currentTimeMillis();
        System.out.println("End:" + end + " Append:" + (end - begin));
        
        System.out.println("-------------华丽的分割线--------------");
        
        long begin2 = System.currentTimeMillis();
        System.out.println("Begin:" + begin2);
        RunFunCursor cursor2 = new RunFunCursor();
        cursor2.runSy(3);
        long end2 = System.currentTimeMillis();
        System.out.println("End:" + end2 + " Append:" + (end2 - begin2));
    }
}
